<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0 minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>时间题</title>
    <link href="public/css/header.css" rel="stylesheet" type="text/css">
    <link href="public/css/container.css" rel="stylesheet" type="text/css">
    <link href="public/css/question.css" rel="stylesheet" type="text/css">
    <link href="public/css/call_back.css" rel="stylesheet" type="text/css">
    <script src="../public/jQuery/jQuery-2.1.3.min.js"></script>
    <script src="../public/js/publicjs.js"></script>
    <script src="public/js/myjs/whole.js"></script>
    <style>

    </style>
</head>
<body>

<header>
    <div class="header__box tl">
        <img onclick="goback()" src="public/img/basic/back.png"/>
        <span onclick="goback()" class="back">返回</span>
    </div>
    <div class="header__box tc header__box__title">
        时间题
    </div>

    <div class="pop-up">
    </div>

</header>
<section style="background: #EDF0F0;">
    <div class="text_div_sub_title">
        题目标题
    </div>
    <div class="text_div_input">
        <input id="question_body" class="input_title" type="text" placeholder="请输入题目名称（必填），最多20字"/>
        <div style="width: 100%;height: 1px;background: #E5E5E5;position: relative;"></div>
        <div class="div_img_title">
            <img onclick="pic_func();" src="public/img/subject/camera.png">
            <form name="form" id="form" hidden>
                <input type="file" name="pic" class="pic" id="pic" multiple="multiple">
            </form>
            <!--<input type="file"  name="pic" id="pic" accept="image/*" style="display: none;">-->
        </div>

        <div class="div_img_img">

        </div>
        <div style="clear: both;">

        </div>

    </div>
    <div class="text_div_option">
        设置
    </div>

    <div class="div_setting_is_ness1">

        <div class="text_setting_is_ness">
            此题目可跳过不答
        </div>
        <div class="setting">
            <img src="public/img/subject/choose-off.png" data-ischoose="1" class="multi-img" id="must-cho">
        </div>
    </div>
    <div class="div_sure">
        <div onclick="submit();" class="div_sure_click">
            确定
        </div>
    </div>
    <input type="hidden" id="questionnaire_id"/>
    <input type="hidden" id="sequence"/>
    <input type="hidden" id="question_type" value="5"/>
    <input type="hidden" id="question_id" value="0"/>
    <input type="hidden" id="questionnaire_name" value="0"/>
    <input type="hidden" id="questionnaire_welcome" value="0"/>
    <input type="hidden" id="type" value="0"/>
</section>
</body>
</html>
<script>
    $(function () {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        };

        //进入页面,首先拿到传过来的questionnaire_id
        var questionnaire_id = $.getUrlParam('questionnaire_id');

        var sequence = $.getUrlParam('sequence');
        //问题id
        var question_id = $.getUrlParam('question_id');

        var questionnaire_name = unescape($.getUrlParam('questionnaire_name'));
        var questionnaire_welcome = unescape($.getUrlParam('questionnaire_welcome'));

        var type = $.getUrlParam('type');
        $("#type").val(type);

        //保存标题和欢迎词
        $('#questionnaire_name').val(questionnaire_name);
        $('#questionnaire_welcome').val(questionnaire_welcome);

        $("#questionnaire_id").val(questionnaire_id);

        $("#sequence").val(sequence);

        $("#question_id").val(question_id);

        if (question_id == -1) {

        }
        else {

            API_Func('/questionnaires/questionnaireQuestion/' + question_id, 'GET', {id: question_id}, true, function () {

            }, function (d) {
                var data = d.data;
                //图片
                var imgs_array = [];
                var imgs_html = "";

                if (data.question_img != "") {

                    imgs_array = data.question_img.split(',');

                    for (var i = 0; i < imgs_array.length; i++) {
                        imgs_html += '<div><img class="img_up" src="' + imgs_array[i] + '">' +
                                '<img class="red_delete" src="public/img/subject/redDelete.png"/>' +
                                '</div>';
                    }
                }
                if (imgs_html == "") {

                }
                else {
                    $('.div_img_img').append(imgs_html);
                }
                //标题
                var title= INPUTEncode(data.question_body);
                $('#question_body').val(title);

                //是否必须
                var is_necessary = data.is_necessary;
                var must_cho = $("#must-cho");
                if (is_necessary == 0) {
                    must_cho.attr('data-ischoose', '0');
                    must_cho.attr('src', 'public/img/subject/choose-on.png');
                }
                else {
                    must_cho.attr('data-ischoose', '1');
                    must_cho.attr('src', 'public/img/subject/choose-off.png');
                }

                //删除图片
                $('.red_delete').unbind('click').on('click', function () {
                    var _self = $(this);
                    _self.parent().remove();
                });

            });
        }
    });

    $("#must-cho").click(function () {
        var ischoose = $(this).attr('data-ischoose');
        if (ischoose == 0) {
            $(this).attr('data-ischoose', '1');
            $(this).attr('src', 'public/img/subject/choose-off.png');
        }
        else {
            $(this).attr('data-ischoose', '0');
            $(this).attr('src', 'public/img/subject/choose-on.png');
        }
    });
    function submit() {

        //问卷id
        var questionnaire_id = $("#questionnaire_id").val();

        //排序
        var sequence = $("#sequence").val();

        //是否必填
        var is_necessary = $('#must-cho').attr('data-ischoose');

        //问题主干
        var question_body = $('#question_body').val();

        //图片
        var question_img = '';
        $('.div_img_img div .img_up').each(function () {
            if ($(this).attr('src') != "") {
                question_img += $(this).attr('src') + ',';
            }
        });
        question_img = question_img.substr(0, question_img.length - 1);

        var pop_up = $('.pop-up');
        if (question_body == '') {
            pop_up_fn(pop_up, '标题不能为空');
            return;
        }
        if (question_body.length > 20) {
            pop_up_fn(pop_up, '标题字数不能超过20字');
            return;
        }

        //题目类型
        var question_type = $('#question_type').val();

        //打分上限
        //var select_limit = $('.score_limit').attr('data-select_limit');

        question_body = HTMLEncode(question_body);
        var data = {
            is_necessary: is_necessary,
            question_body: question_body,
            question_img: question_img,
            question_options: "0",
            question_type: question_type,
            mark_limit: "0",
            select_limit: '0',
            rows: '0',
            sequence: sequence
        };

        var jsonString = JSON.stringify(data);

        var question_id = $("#question_id").val();
        //如果问题ID为-1的话做添加操作
        if (question_id == -1) {

            API_Func('/questionnaires/' + questionnaire_id + '/addQuestion', 'POST', {jsonString: jsonString}, false, function () {
                $('.div_sure_click').text('正在添加...');
            }, function (d) {
                $('.div_sure_click').text('确定');

                if (d.code == 0) {
                    console.log(d);
                    pop_up_fn(pop_up, '添加成功');
                    back_edit();
//                    setTimeout("location.href='edit.html?questionnaire_id='+$('#questionnaire_id').val()", 1000);
                }
                else {
                    pop_up_fn(pop_up, '添加失败');
                }
            });
        }
        else {
            API_Func('/questionnaires/questionnaireQuestion/' + question_id, 'PUT', {
                id: question_id,
                jsonString: jsonString
            }, false, function () {
                $('.div_sure_click').text('正在修改...');
            }, function (d) {
                $('.div_sure_click').text('确定');

                if (d.code == 0) {
                    pop_up_fn(pop_up, '修改成功');
                    back_edit();
//                    setTimeout("location.href='edit.html?questionnaire_id='+$('#questionnaire_id').val()",1000);
                }
                else {
                    pop_up_fn(pop_up, '修改失败');
                }
            });
        }

    }
    function pic_func() {
        $('#pic').click();
    }
    function back_edit() {

        setTimeout("b();", 1000);
    }
    function b() {
        var questionnaire_name = $('#questionnaire_name').val();
        var questionnaire_welcome = $('#questionnaire_welcome').val();
        var questionnaire_id = $('#questionnaire_id').val();
        var typeUrl = $("#type").val();
        location.href = 'edit.html?questionnaire_id=' + questionnaire_id + "&questionnaire_name=" + escape(questionnaire_name) + "&questionnaire_welcome=" + escape(questionnaire_welcome) + "&type=" + typeUrl;
    }
</script>